﻿// JScript 文件
//var tc = null;
//function init()
//{
//    tc = new CtnTreeCtrl("tc", 0, -1);
//    tc.onSelectItem = function (item)
//    {
//        if (item)
//        {
//            alert(item.data.value)
//        }
//    }
//    tc.onRightMenu = function (evt, item){alert("右键菜单");}
//    tc.onExpand = function (item)
//    {
//        var idx = item.idx;
//        if (!item.isdeal)
//        {
//            if (!(idx % 5))
//            {
//                item.show();
//                return;
//            }
//            
//            if (idx % 2)
//            {
//                for (var i = 0 ; i < 3; i++)
//                {
//                    item.insertItem("节点" + idx + "_" + i, {value:idx + "_" + i});//存对象
//                    //item.insertItem("节点" + idx + "_" + i, idx);//存单数据
//                }
//            }
//            else
//            {
//                item.deleteItem(item.idx);
//                return;
////                var xmlinfo = new String();
////                xmlinfo += '<items>';
////                for (var i = 0 ; i < 5; i++)
////                {
////                    xmlinfo += '<item icon="0" bsort="1" bcheckbox="1">';//icon:图标索引号，bsort:-1降序，0不排序，1升序
////                    xmlinfo += '<text>node'+ idx + '_' + i + '</text>';
////                    xmlinfo += '<data value="' + idx + '_' + i + '"></data>';//存对象
////                    //xmlinfo += '<data>' + idx + '_' + i + '</data>';//存单数据
////                    xmlinfo += '<child>';
////                        for (var j = 0 ; j < 3; j++)
////                        {
////                            xmlinfo += '<item icon="1" bend="1" oricheck="1" bcheckbox="1">';//bend:是否结束，bcheckbox:是否选择框,bchecked:-1未选，0半选，1选中
////                            xmlinfo += '<text>node'+ idx + '_' + j + '</text>';
////                            xmlinfo += '<data value="' + idx + '_' + j + '" type="3"></data>';//存对象
////                            //xmlinfo += '<data>' + idx + '_' + j + '</data>';//存单数据
////                            xmlinfo += '</item>';
////                        }
////                    xmlinfo += '</child>';
////                    xmlinfo += '</item>';
////                }
////                xmlinfo += '</items>';
////                item.insertXml(xmlinfo);
//            }
//            item.show();
//        }
//    }
//    tc.create("div1");
//    tc.setImagePath("../Images/treeimg");
//    tc.setIcons("../Images/treeimg/test", ['tux.png','user.png','comment.png','home.gif']);//设置自定义图标索引
//    var node = tc.insertItem("根节点",{value:"根节点"});
//    //node.icon = 3;
//    tc.show();
//}

CtnTreeCtrl.images = {
    proot: "P3.gif",
    mroot: "M3.gif",
    lroot: "L3.gif",
    
    ptop: "P0.gif",
    mtop: "M0.gif",
    ltop: "L0.gif",
    pcenter: "P1.gif",
    pbottom: "P2.gif",
    mcenter: "M1.gif",
    mbottom: "M2.gif",
    lcenter: "L1.gif",
    lbottom: "L2.gif",
    
    sbling: "L4.gif",
    space: "mp.gif",
    
    root: "root.gif",
    open: "open.gif",
    close: "close.gif",
    file: "file.gif",
    loading: "loading.gif",
    
    checked: "ckb0.gif",
    unchecked: "ckb1.gif",
    halfchecked: "ckb2.gif"
}

CtnTreeCtrl.treeIdx = 0;
CtnTreeCtrl.TreeItem = TreeItem_TC;
function CtnTreeCtrl(treename, bcheckbox, bsort)
{
    this.treename   = treename;
    this.treeid     = null;
    this.rootcss = "treectrl";
    this.csssel = "sel";
    this.images = null;//加减图标
    this.imagePath = "../images/treeImg";
    this.iconKey    = null;//设置图标的关键字
    this.iconKeyData = null;//设置图标的数据{value:dd, iconidx:kk}
    this.openIcons   = null;//打开时的自定义图标
    this.closeIcons  = null;//关闭时的自定义图标
    this.selectedItem = null;
    //this.arrAllItem = new Array();//所有节点
    //接口
    this.onCheckItem = null;
    this.onSelectItem = null;
    this.onDblClick = null;
    this.onExpand = null;
    this.onRightMenu = null;
    
    this.onShowMsg = null;//显示消息(msg)
    this.getSelectedItem = getSelectedItem_TC;
    
    this.selectItem     = selectItem_TC;
    this.expand         = expand_TC;
    this.rightMenu      = rightMenu_TC;
    this.checkItem      = checkItem_TC;
    
    this.setImagePath = setImagePath_TC;
    this.setIcons = setIcons_TC;
    this.setIconByKey = setIconByKey_TC;
    
    //拷贝对象
    var item = new CtnTreeCtrl.TreeItem();
    for (var key in item)
    {
        this[key] = item[key];//拷贝对象
    }
    this.bcheckbox  = bcheckbox;
    this.bsort      = bsort;
    this.idx        = CtnTreeCtrl.treeIdx++;
    this.root = this;
    this.setIcons();
}

function TreeItem_TC()
{
    this.parent = null;
    this.bcheckbox  = 0;
    this.bsort  = 0;
    this.text   = null;
    this.data   = {};
    this.level  = 0;
    this.icon    = null;
    this.bend   = 0;//
    this.bchecked = 0;
    
    this.arrItem    = new Array(); //子树
    this.idx        = 0;    //本节点的索引号    this.bexpand    = false;    //节点是否展开
    this.isdeal     = false;    //是否处理过    this.root       = null;//根节点    this.oricheck = 0;
    
    //公用函数
    this.create         = create_TC;    //初始化//    this.setImage       = setImage_TC;//设置
    this.insertItem     = insertItem_TC;//插入节点
    this.insertByShow   = insertByShow_TC;//不用show
    this.deleteItem     = deleteItem_TC;//删除节点
    this.setItemData    = setItemData_TC;//设置节点数据
    this.getItemData    = getItemData_TC;//获取节点数据
    this.setItemText    = setItemText_TC;
    this.getItemText    = getItemText_TC;
    this.findItem       = findItem_TC;  //根据Id查找遍历节点
    this.findItemArrByKey  = findItemArrByKey_TC;//根据数据
    this.getAllItem     = getAllItem_TC;
    this.getItemPos     = getItemPos_TC; //获得节点引索号    this.show           = show_TC;//显示
    this.setCheck       = setCheck_TC;
    this.insertXml      = insertXml_TC;//
    this.insertNode     = insertNode_TC;
    this.loading        = loading_TC;
    this.unloading      = unloading_TC;
    this.getCheckedArr  = getCheckedArr_TC;
    this.getAllChecked  = getAllChecked_TC;
    this.getCheckedByKey = getCheckedByKey_TC;//(key, value, obj)
    this.clear          = clear_TC;
    this.expandAll      = expandAll_TC;//展开所有
     //xml
    this.getIcon        = getIcon_TC;
    this.getNodeImage = getNodeImage_TC;
    this.getSpaceImages = getSpaceImages_TC;
    this.getCheckbox = getCheckbox_TC;
    this.$ = $_TC;
    this.$XML = $XML_TC;
    this.getXmlDoc      = getXmlDoc_TC;
    this.clearSpace     = clearSpace_TC;
    this.sortItem       = sortItem_TC;
    this.showMsg        = showMsg_TC;
}
function showMsg_TC(msg)
{
    if (this.root.onShowMsg)
    {
        this.root.onShowMsg(msg);
    }
    else
    {
        alert(msg);
    }
}
function setItemText_TC(text)
{
    this.text = text;
    this.$("t" + this.idx).innerHTML = text;
}
function getItemText_TC()
{
    return this.text;
}

function clear_TC()
{
    while(this.arrItem.length > 0)
    {
        this.arrItem.pop();
    }
}
function getAllItem_TC()
{
    var arrItem = new Array();
    for (var i = 0; i < this.arrItem.length; i++)
    {
        var arrTmp = this.arrItem[i].getAllItem();
        arrItem = arrItem.concat(arrTmp);
    }
    if (this.parent)
    {
        arrItem.push(this);
    }
    return arrItem;
}
function getAllChecked_TC(obj)
{
    if (!obj[0])
    {
        for (var i = -2; i <= 2; i++)
        {
            obj[i] = new Array();
        }
    }
    for (var i = 0; i < this.arrItem.length; i++)
    {
        this.arrItem[i].getAllChecked(obj);
    }
    if (this.parent)
    {
        obj[this.bchecked - this.oricheck].push(this);
    }
}
function findItemArrByKey_TC(key, value)
{
    var arrItem = new Array();
    for (var i = 0; i < this.arrItem.length; i++)
    {
        var arrTmp = this.arrItem[i].findItemArrByKey(key, value);
        arrItem = arrItem.concat(arrTmp);
    }
    if (((key!=null) && (this.data[key] == value))
        || ((key == null) && (this.data == value)))
    {
        arrItem.push(this);
    }
    return arrItem;
}
function getCheckedByKey_TC(key, value, obj)
{
    if (!obj[0])
    {
        for (var i = -2; i <= 2; i++)
        {
            obj[i] = new Array();
        }
    }
    for (var i = 0; i < this.arrItem.length; i++)
    {
        this.arrItem[i].getAllChecked(obj);
    }
    if (this.parent && (((key != null) && (this.data[key] == value)) || ((key == null) && (this.data == value))))
    {
        obj[this.bchecked - this.oricheck].push(this);
    }
}

function getRiseByKey_TC(key, value)
{
    var arr = new Array();
    var arrAllItem = this.findItemArrByKey(key, value);
    for (var i = 0; i < arrAllItem.length; i++)
    {
        if (arrAllItem[i].bchecked - arrAllItem[i].oricheck > 0)
        {
            arr.push(arrAllItem[i]);
        }
    }
    return arr;
}

function getFallByKey_TC(key, value)
{
    var arr = new Array();
    var arrAllItem = this.findItemArrByKey(key, value);
    for (var i = 0; i < arrAllItem.length; i++)
    {
        if (arrAllItem[i].bchecked - arrAllItem[i].oricheck < 0)
        {
            arr.push(arrAllItem[i]);
        }
    }
    return arr;
}

function getCheckedArr_TC(stat)
{
    var arrItem = new Array();
    for (var i = 0; i < this.arrItem.length; i++)
    {
        if (this.arrItem[i].bchecked == stat)
        {
            arrItem.push(this.arrItem[i]);//根据状态选
        }
        else if ((stat == null) && (this.arrItem[i].bchecked == 1))
        {
            arrItem.push(this.arrItem[i]);//取全选
        }
    }
    return arrItem;
}
function sortItem_TC()
{
    if (this.bsort == -1)
    {
        this.arrItem.sort(
                            function(left, right)
                            {
                                var a = left.text, b = right.text;
                                return a > b ? -1 : a < b ? 1 : 0;
                            }
                           );
    }
    else if (this.bsort == 1)
    {
        this.arrItem.sort(
                            function(left, right)
                            {
                                var a = left.text, b = right.text;
                                return a < b ? -1 : a > b ? 1 : 0;
                            }
                           );
    }
}

function loading_TC()
{
    this.isdeal = true;
    this.arrItem.push(1);
    this.$("i" + this.idx).src = this.getNodeImage();
    this.$("p" + this.idx).src = this.getIcon();
    elem = this.$('c' + this.idx);
    elem.innerHTML = this.getSpaceImages() + '<img src="' + this.root.images['loading'] + '" alt=""/> ' + '加载中...';
    elem.show();
    this.isdeal = false;
}
function unloading_TC()
{
    this.arrItem.splice(0, 1);
    elem = this.$('c' + this.idx);
    elem.innerHTML = '';
    elem.hide();
}
function getSelectedItem_TC()
{
    return this.selectedItem;
}
function $XML_TC(xmlNode)
{
    if (!xmlNode)
    {
        return null;
    }
    var vText = null;
    
    if (xmlNode.text)
    {
        vText = xmlNode.text;
    }
    else
    {
        vText = xmlNode.textContent;
    }
    
    return vText;
}
function getXmlDoc_TC()
{
    var XmlVal = null ;
    try
    {
        XmlVal = new ActiveXObject("Msxml2.DOMDocument.4.0") ;
    }
    catch(e)
    {
        try
        {
            XmlVal = new ActiveXObject("Microsoft.XMLDOM") ;
        }
        catch(o)
        {
            if(document.implementation 
                && document.implementation.createDocument
                && document.implementation.hasFeature("XPath", "3.0"))
            {
                XmlVal = document.implementation.createDocument('','',null);
                XmlVal.loadXML = function(xmlString)
                {
                    var childNodes = this.childNodes;
                    for (var i = childNodes.length - 1; i >= 0; i--)
                        this.removeChild(childNodes[i]);

                    var dp = new DOMParser();
                    var newDOM = dp.parseFromString(xmlString, "text/xml");
                    var newElt = this.importNode(newDOM.documentElement, true);
                    this.appendChild(newElt);
                }
            }
        }
    }
    
    return XmlVal;
}

function clearSpace_TC(elem)
{
    if(!elem.nodeType)
    {
        return;
    }
    for (var i = elem.childNodes.length - 1; i >= 0 ; i--)
    {
        if (elem.childNodes[i].nodeType == 3)
        {
            var text = this.$XML(elem.childNodes[i]);
            text = text.replace(new RegExp("(^[ \f\n\r\t\v]*)|([ \f\n\r\t\v]*$)","g"),"");
            if (!text)
            {
                elem.removeChild(elem.childNodes[i]);
            }
        }
    }
}

function insertXml_TC(xmlinfo, params)
{
    var xmlVal = this.getXmlDoc();
//    if (xmlinfo.indexOf("<?xml") == -1)
//    {
//        xmlinfo = "<?xml version='1.0' encoding='gb2312' ?> " + xmlinfo;
//    }
    xmlVal.loadXML(xmlinfo);
    this.clearSpace(xmlVal);
    if (xmlVal.childNodes.length < 1 || xmlVal.childNodes[0].tagName == "parsererror")
    {
        this.showMsg("数据不合法！");
        return false;
    }
    var rootNode = null;
    if (xmlVal.childNodes[0].nodeName != "xml")
    {
        rootNode = xmlVal.childNodes[0];
    }
    else if (xmlVal.childNodes.length > 1)
    {
        rootNode = xmlVal.childNodes[1];
    }
    this.insertNode(rootNode);
    //this.show();
}

function insertNode_TC(node, params)
{
    this.clearSpace(node);
    for (var i = 0; i < node.childNodes.length; i++)
    {
        var text = "";
        var data = {};
        var child = null;
        
        var li = node.childNodes[i];
        this.clearSpace(li);
        for (var j = 0; j < li.childNodes.length; j++)
        {
            if (li.childNodes[j].nodeName == "text")
            {
                text = this.$XML(li.childNodes[j]);
            }
            else if (li.childNodes[j].nodeName == "data")
            {
                if (this.$XML(li.childNodes[j]))
                {
                    data = this.$XML(li.childNodes[j]);
                }
                else
                {
                    var attrs = li.childNodes[j].attributes;
                    for (var k = 0; k < attrs.length; k++)
                    {
                        data[attrs[k].name] = attrs[k].value;
                    }
                }
            }
            else if (li.childNodes[j].nodeName == "child")
            {
                child = li.childNodes[j];
            }
        }
        
        var tree = this.insertItem(text, data);
        for (var j = 0; j < li.attributes.length; j++)
        {
            tree[li.attributes[j].name] = li.attributes[j].value;
        }
        if (child)
        {
            tree.insertNode(child, params);
        }
        if (params)
        {
            for (var key in params)
            {
                tree[key] = params[key];
            }
        }
    }
}
//flag:-1父级，0:本级, 1子级, null: 全部
function setCheck_TC(stat, flag)
{
    //flag = flag==null?-1:flag;
    switch (flag)
    {
        case -1:
        {
            var checked = 0;
            var uncheck = 0;
            for (var i = 0; i < this.arrItem.length; i++)
            {
                if (this.arrItem[i].bchecked == 1)
                {
                    checked++;
                }
                else if (this.arrItem[i].bchecked == -1)
                {
                    uncheck++;
                }
            }
            if (checked == this.arrItem.length)
            {
                stat = 1;
            }
            else if(uncheck == this.arrItem.length)
            {
                stat = -1;
            }
            else
            {
                stat = 0;
            }
            this.bchecked = stat;
            this.$("k" + this.idx).src = this.getCheckbox();
            if (this.parent && this.parent.bcheckbox)
            {
                this.parent.setCheck(0, -1);
            }
        }break;
        case 0:
        {
            this.bchecked = stat;
            this.$("k" + this.idx).src = this.getCheckbox();
        }break;
        case 1:
        {
            for (var i = 0; i < this.arrItem.length; i++)
            {
                this.arrItem[i].setCheck(stat, 1);
            }
            this.bchecked = stat;
            this.$("k" + this.idx).src = this.getCheckbox();
        }break;
        default:
        {
            for (var i = 0; i < this.arrItem.length; i++)
            {
                this.arrItem[i].setCheck(stat, 1);
            }
            this.bchecked = stat;
            this.$("k" + this.idx).src = this.getCheckbox();
            if (this.parent && this.parent.bcheckbox)
            {
                this.parent.setCheck(0, -1);
            }
        }break;
    }
}

function checkItem_TC(idx)
{
    var item = this.findItem(idx);
    if (this.onCheckItem)
    {
        if(this.onCheckItem(item))
        {
            return ;
        };
    }
    if (this.selectedItem)
    {
        var elem = this.$("t" + this.selectedItem.idx);
        if (elem)
        {
            elem.className = "";
        }
    }
    this.selectedItem = item;
    this.$("t" + this.selectedItem.idx).className = this.csssel;
    if (item.bchecked == 1)
    {
        item.setCheck(-1);
    }
    else
    {
        item.setCheck(1);
    }
}
function expandAll_TC(flag)
{
    this.bexpand = flag == null?false:!flag;
    for (var i = 0; i < this.arrItem.length; i++)
    {
        this.arrItem[i].expandAll(flag);
    }
    this.root.expand(this.idx);
}
function expand_TC(idx)
{
    var item = this.findItem(idx);
    if (item.bexpand)
    {
        item.bexpand = false;
        if (item.arrItem.length > 0)
        {
            item.$("i" + idx).src = item.getNodeImage();
            item.$("p" + idx).src = item.getIcon();
            item.$("c" + idx).hide();
        }
        return;
    }
    else
    {
        item.bexpand = true;
        if (item.isdeal)
        {
            item.$("i" + idx).src = item.getNodeImage();
            item.$("p" + idx).src = item.getIcon();
            item.$("c" + idx).show();
        }
        else if (!this.onExpand)
        {
            item.show();
        }
    }
    if (this.onExpand)
    {
        this.onExpand(item);
    }
}
function rightMenu_TC(evt, idx)
{
    if (evt.button != 2)
    {
        return ;//不是右键返回
    }
    var item = this.findItem(idx);
    if (this.selectedItem)
    {
        var elem = this.$("t" + this.selectedItem.idx);
        if (elem)
        {
            elem.className = "";
        }
    }
    this.selectedItem = item;
    this.$("t" + this.selectedItem.idx).className = this.csssel;
    if (this.onRightMenu)
    {
        this.onRightMenu(item, evt);
    }
}

function selectItem_TC(idx)
{
    var item = this.findItem(idx);
    if (this.selectedItem)
    {
        var elem = this.$("t" + this.selectedItem.idx);
        if (elem)
        {
            elem.className = "";
        }
    }
    this.selectedItem = item;
    this.$("t" + this.selectedItem.idx).className = this.csssel;
    if (this.onSelectItem)
    {
        this.onSelectItem(item);
    }
}

function setImagePath_TC(path)
{
    if (!path)
    {
        path = this.imagePath;
    }
    else
    {
        this.imagesPath = path;
    }
    var images = CtnTreeCtrl.images;
    this.images = {};
    for (var key in images)
    {
        this.images[key] = path + "/" + images[key];
        new Image().src = this.images[key];//开辟缓存
    }
}

function setIcons_TC(path, openIcons, closeIcons)
{
    if (!path)
    {
        path = "";
    }
    if (!this.images)
    {
        this.setImagePath();
    }
    var images = this.images;
    this.openIcons = {root: images.root, folder:images.open, file:images.file};//先储存默认的几项
    this.closeIcons = {root: images.root, folder:images.close, file:images.file};
    if (!openIcons)
    {
        return;
    }
    for (var key in openIcons)
    {
        this.openIcons[key] = path + "/" + openIcons[key];
        new Image().src = this.openIcons[key];//开辟缓存
    }
    if (closeIcons)
    {
        for (var key in closeIcons)
        {
            this.closeIcons[key] = path + "/" + closeIcons[key];
            new Image().src = this.closeIcons[key];//开辟缓存
        }
    }
    else
    {
        for (var key in openIcons)
        {
            this.closeIcons[key] = path + "/" + openIcons[key];
            new Image().src = this.closeIcons[key];//开辟缓存
        }
    }
}

function setIconByKey_TC(key, params)
{
    this.iconKey = key;
    this.iconkeyData = params;
}

function getIcon_TC()
{
    if(!this.parent)
    {
        return "";
    }
    var icons = null;
    if (this.bexpand)
    {
        icons = this.root.openIcons;
    }
    else
    {
        icons = this.root.closeIcons;
    }
    var icon = null;
    if (this.icon!=null)
    {
        icon = icons[this.icon];
    }
    else if (this.root.iconKey)
    {
        var keyValue = this.data[this.root.iconKey];
        var iconkeyData = this.root.iconkeyData;
//        for (var i=0; i < iconkeyData.length; i++)
//        {
//            if (iconkeyData[i][1] == keyValue)
//            {
//                icon = icons[iconkeyData[i][0]];
//                break;
//            }
//        }
        for (var key in iconkeyData)
        {
            if (key == keyValue)
            {
                icon = icons[iconkeyData[key]];
                break;
            }
        }
    }
    if (!icon)
    {
        if(!this.parent.parent)
        {
            icon = icons['root'];
        }
        else if ((this.isdeal && this.arrItem.length < 1)|| this.bend)
        {
            icon = icons['file'];
        }
        else// if (!this.isdeal)
        {
            icon = icons['folder'];
        }
    }
    
    return icon;
}
function getNodeImage_TC()
{
    var image = null;
    if (this.bexpand)
    {
        if (!this.parent)
        {
            image = this.root.images['mroot'];
        }
        else if ((this.isdeal && this.arrItem.length < 1)|| this.bend)
        {
            if (this.parent.arrItem.length == this.getItemPos() +1)
            {
                image = this.root.images['lbottom'];
            }
            else
            {
                image = this.root.images['lcenter'];
            }
        }
        else
        {
            if (this.parent.arrItem.length == this.getItemPos() +1)
            {
                image = this.root.images['mbottom'];
            }
            else
            {
                image = this.root.images['mcenter'];
            }
        }
    }
    else
    {
        if (!this.parent)
        {
            image = this.root.images['proot'];
        }
        else if ((this.isdeal && this.arrItem.length < 1)|| this.bend)
        {
            if (this.parent.arrItem.length == this.getItemPos() +1)
            {
                image = this.root.images['lbottom'];
            }
            else
            {
                image = this.root.images['lcenter'];
            }
        }
        else
        {
            if (this.parent.arrItem.length == this.getItemPos() +1)
            {
                image = this.root.images['pbottom'];
            }
            else
            {
                image = this.root.images['pcenter'];
            }
        }
    }
    
//    return '<img id="i' + this.idx + '" src="' + image + '" alt="" onclick="' + this.root.treename +'.expand(' + this.idx + ');"/>';
    return image;
}

function getSpaceImages_TC()
{
    var vHtml = "";
    if (this.level < 2)
    {
        return "";
    }
    else
    {
        vHtml += this.parent.getSpaceImages();
        if (this.parent.arrItem.length == this.getItemPos() +1)//如果父结点是最后的节点
        {
            vHtml += '<img src="' +  this.root.images['space'] + '" alt="" />';
        }
        else
        {
            vHtml += '<img src="' +  this.root.images['sbling'] + '" alt="" />';
        }
    }
    return vHtml;
}

function getCheckbox_TC()
{
    var ckb = null;
    switch (parseInt(this.bchecked))
    {
        case -1: ckb = this.root.images['unchecked'];break;
        case 0:  ckb = this.root.images['halfchecked'];break;
        case 1: ckb = this.root.images['checked'];break;
        default:break;
    }
    return ckb;
}

function create_TC(id)
{
    this.treeid = id;
    var vHtml ='<div id="d' + this.idx + '" style="display:none">';
    vHtml += '<img id="i' + this.idx + '"/>';
    vHtml += '<img id="p' + this.idx + '" />';
    vHtml += '<img id="k' + this.idx + '"/>';
    vHtml += '<a id="t' + this.idx + '"></a></div>';
    vHtml += '<div id="c' + this.idx + '" class="' + this.rootcss + '">';
    vHtml += '</div>';
    this.$(this.treeid).innerHTML = vHtml;
}

function insertItem_TC(text, data)
{
    var tree = {};
    if(!this.parent && this.arrItem)//
    {
        var item = new CtnTreeCtrl.TreeItem();//返回第一级
        for (var key in item)
        {
            item[key] = this[key];//拷贝对象
        }
        tree = item;
    }
    else
    {
        for (var key in this)
        {
            tree[key] = this[key];
        }
    }
    
    if (tree != this)
    {
        tree.parent = this;
        tree.level = this.level + 1;
        this.arrItem.push(tree);
    }
    tree.idx = CtnTreeCtrl.treeIdx++;
    tree.arrItem = new Array();
    tree.isdeal = false;
    tree.bexpand = false;
    tree.icon = null;
    //tree.bchecked = -1;
    //tree.oricheck = -1;
    tree.text = text;
    tree.data = data!=null?data:{};
    return tree;
}
function insertByShow_TC(text, data)
{
    var tree = this.insertItem(text, data);
    if (this.bsort)
    {
        this.sortItem();
    }
    
    this.$("i" + this.idx).src = this.getNodeImage();
    this.$("p" + this.idx).src = this.getIcon();
    
    var arrItem = this.arrItem;
    for (var i = 0; i < arrItem.length; i++)
    {
        if (this.arrItem[i] == tree)
        {
            var vHtmlSpace = this.getSpaceImages();//"";
            var htmlBuf = new Array();
            htmlBuf.push('<div id="d' + arrItem[i].idx + '">');
            htmlBuf.push(vHtmlSpace);
            htmlBuf.push('<img id="i' + arrItem[i].idx + '" src="' + arrItem[i].getNodeImage() + '" alt="" onclick="' + this.root.treename +'.expand(' + arrItem[i].idx + ');"/>');
            htmlBuf.push('<img id="p' + arrItem[i].idx + '" src="' + arrItem[i].getIcon() + '" alt="" ondblclick="' + this.root.treename +'.expand(' + arrItem[i].idx + ');"/>');
            if ((arrItem[i].bcheckbox== true) || (arrItem[i].bcheckbox == 1))
            {
                if (((this.bcheckbox== true) || (this.bcheckbox == 1)) && this.bchecked != 0)
                {
                    arrItem[i].bchecked = this.bchecked;
                    arrItem[i].oricheck = this.bchecked;
                }
                else
                {
                    arrItem[i].bchecked = arrItem[i].oricheck;
                }
                htmlBuf.push('<img id="k' + arrItem[i].idx + '" src="' + arrItem[i].getCheckbox() + '" alt=""  onclick="' + this.root.treename +'.checkItem(' + arrItem[i].idx + ');"/>');
            }
            htmlBuf.push('<a href="javascript:' + this.root.treename + '.selectItem(' + arrItem[i].idx + ');"');
            htmlBuf.push(' id="t' + arrItem[i].idx + '"');
            htmlBuf.push(' title="' + arrItem[i].text + '"');
            htmlBuf.push(' onmousedown="' + this.root.treename + '.rightMenu(event, ' + arrItem[i].idx + ');">');
            htmlBuf.push(arrItem[i].text);
            htmlBuf.push('</a></div>');
            htmlBuf.push('<div id="c' + arrItem[i].idx + '" style="display:none">');
            htmlBuf.push('</div>');
            if (i == 0)//如果是第一个
            {
                this.$('c' + this.idx).insertAdjacentHTML("afterbegin", htmlBuf.join(""));
            }
            else
            {
                this.$('c' + arrItem[i - 1].idx).insertAdjacentHTML("afterend", htmlBuf.join(""));
            }
            break;
        }
        this.$('c' + this.idx).show();
    }
    return tree;
}
function deleteItem_TC(idx)
{
    var item = idx;//重载
    if (typeof(item) != "object")
    {
        item = this.root.findItem(idx);
    }
    if (item)
    {
        var parent = item.parent;
        if (parent)
        {
            var i = parent.getItemPos(idx);
            if (i > -1)
            {
                if (i == parent.arrItem.length - 1)//
                {
                    var elem = this.$("i" + parent.idx)
                    if (elem)
                    {
                        elem.src = parent.getNodeImage();
                    }
                }
                
//                var allItem = this.root.arrAllItem;
//                for (var j = 0; j < allItem.length; j++)
//                {
//                    if (allItem[j] == item)
//                    {
//                        arrItem.splice(j, 1);
//                        break;
//                    }
//                }
                
                parent.arrItem.splice(i, 1);
                var elem = this.$('d' + idx);
                if (elem)
                {
                    elem.parentNode.removeChild(elem);
                }
                elem = this.$('c' + idx);
                if (elem)
                {
                    elem.parentNode.removeChild(elem);
                }
                if (parent.arrItem.length == 0)
                {
                    this.$('c' + parent.idx).hide();
                }
            }
        }
    }
}
function show_TC()
{
    this.isdeal = true;
    //如果是根节点
//    if (!this.parent)
//    {
//        var vHtml = '<div id="c' + this.idx + '" class="' + this.rootcss + '">';
////        vHtml += '<div id="d' + this.idx + '">';
////        vHtml += '<img id="i' + this.idx + '" src="' + this.getNodeImage() + '" alt="" onclick="' + this.treename +'.expand(' + this.idx + ');" style="display:none;"/>';
////        vHtml += '<img id="p' + this.idx + '" src="' + this.getIcon() + '" alt=""/>';
////        vHtml += '<img id="k' + this.idx + '" src="' + this.getCheckbox() + '" alt="" style="display:none;"/>';
////        vHtml += '<a href="javascript:' + this.treename + '.selectItem(' + this.idx + ');"';
////        vHtml += ' onmousedown="' + this.treename + '.rightMenu(event, ' + this.idx + ');">' + this.text + '</a>';
////        vHtml += '</div>';
////        vHtml += '<div>';
////        vHtml += '</div>';
//        vHtml += '</div>';
//        this.$(this.treeid).innerHTML = vHtml;
//    }
//    else
//    {
        this.$("i" + this.idx).src = this.getNodeImage();
        this.$("p" + this.idx).src = this.getIcon();
//    }
    var arrItem = this.arrItem;
    if (arrItem.length < 1)
    {
        this.$('c' + this.idx).hide();
        return;
    }
    if (this.bsort)
    {
        this.sortItem();
    }
    var vHtmlSpace = this.getSpaceImages();//"";
    var htmlBuf = new Array();
    for (var i = 0; i < arrItem.length; i++)
    {
        htmlBuf.push('<div id="d' + arrItem[i].idx + '">');
        htmlBuf.push(vHtmlSpace);
        htmlBuf.push('<img id="i' + arrItem[i].idx + '" src="' + arrItem[i].getNodeImage() + '" alt="" onclick="' + this.root.treename +'.expand(' + arrItem[i].idx + ');"/>');
        htmlBuf.push('<img id="p' + arrItem[i].idx + '" src="' + arrItem[i].getIcon() + '" alt="" ondblclick="' + this.root.treename +'.expand(' + arrItem[i].idx + ');"/>');
        if ((arrItem[i].bcheckbox== true) || (arrItem[i].bcheckbox == 1))
        {
                if (((this.bcheckbox== true) || (this.bcheckbox == 1)) && this.bchecked != 0)
                {
                    arrItem[i].bchecked = this.bchecked;
                    arrItem[i].oricheck = this.bchecked;
                }
                else
                {
                    arrItem[i].bchecked = arrItem[i].oricheck;
                }
            htmlBuf.push('<img id="k' + arrItem[i].idx + '" src="' + arrItem[i].getCheckbox() + '" alt=""  onclick="' + this.root.treename +'.checkItem(' + arrItem[i].idx + ');"/>');
        }
        htmlBuf.push('<a href="javascript:' + this.root.treename + '.selectItem(' + arrItem[i].idx + ');"');
        htmlBuf.push(' id="t' + arrItem[i].idx + '"');
        htmlBuf.push(' title="' + arrItem[i].text + '"');
        htmlBuf.push(' onmousedown="' + this.root.treename + '.rightMenu(event, ' + arrItem[i].idx + ');">');
        htmlBuf.push(arrItem[i].text);
        htmlBuf.push('</a></div>');
        htmlBuf.push('<div id="c' + arrItem[i].idx + '" style="display:none">');
        htmlBuf.push('</div>');
    }
    this.$('c' + this.idx).innerHTML = htmlBuf.join("");
    this.$('c' + this.idx).show();
    for (var i = 0; i < this.arrItem.length; i++)
    {
        if (this.arrItem[i].arrItem.length > 0)
        {
            this.arrItem[i].bexpand = true;
            this.arrItem[i].show();
            this.root.expand(this.arrItem[i].idx);
        }
    }
    //隐藏节点图标,考虑到效率，移到最后
    if (!this.parent)
    {
        for (var i = 0; i < this.arrItem.length; i++)
        {
            this.$('i' + this.arrItem[i].idx).hide();
        }
    }
    if ((this.bcheckbox== true) || (this.bcheckbox == 1))
    {
        this.setCheck(0, -1);
    }
}

function getItemPos_TC(idx)
{
    var arrItem = null;
    if (idx)
    {
        arrItem = this.arrItem;//获取子节点索引
    }
    else
    {
        arrItem = this.parent.arrItem;//获取自身索引
        idx = this.idx;
    }

    for (var i = 0; i < arrItem.length; i++)
    {
        if (arrItem[i].idx == idx)
        {
            return i;
        }
    }

    return -1;
}

function findItem_TC(idx)
{
    if (this.idx == idx)
    {
        return this;
    }
    for (var i = 0; i < this.arrItem.length; i++)
    {
        var tree = this.arrItem[i].findItem(idx);
        if (tree)
        {
            return tree;
        }
    }
    return null;
}

function setItemData_TC(data)
{
    this.data = data;
}
function getItemData_TC(idx)
{
    if (idx)
    {
        var item = this.root.findItem(idx);
        return item.data;
    }
    return this.data;
}

function $_TC(id)
{
    var elem = document.getElementById(id);
    if (!elem)
    {
        return null;
    }
    if (!document.all)
    {
        elem.insertAdjacentHTML = function(sWhere, sHTML){
			 var df; 
             var r = this.ownerDocument.createRange();
             switch (String(sWhere).toLowerCase()) {
                 case "beforebegin":
                     r.setStartBefore(this);
                     df = r.createContextualFragment(sHTML);
                     this.parentNode.insertBefore(df, this);
                     break;
                 case "afterbegin":
                     r.selectNodeContents(this);
                     r.collapse(true);
                     df = r.createContextualFragment(sHTML);
                     this.insertBefore(df, this.firstChild);
                     break;
                 case "beforeend":
                     r.selectNodeContents(this);
                     r.collapse(false);
                     df = r.createContextualFragment(sHTML);
                     this.appendChild(df);
                     break;
                 case "afterend":
                     r.setStartAfter(this);
                     df = r.createContextualFragment(sHTML);
                     this.parentNode.insertBefore(df, this.nextSibling);
                     break;
                 default:
                    break;
             };
		};
    }
    elem.show = function(){this.style.display="";}
    elem.hide = function(){this.style.display="none";}
    return elem;
}